package org.leetcode.middle.leetcode322;

import java.util.Arrays;

public class Solution {

    public static void main(String[] args) {
//        Solution solution = new Solution();
//        int[] test = {1, 2, 5};
//        int i = solution.coinChange(test, 11);
//
//        System.out.println(i);

        Integer num = Integer.MAX_VALUE;
        Integer num1 =num+1;
        int num2 = Math.min(num, num1);
        System.out.println(num);
        System.out.println(num1);
        System.out.println(num2);
    }

    public int coinChange5(int[] coins, int amount){
        Arrays.sort(coins);
        int [] dp = new int[amount+1];

        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0]=0;

        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j <=amount; j++) {
                dp[j]=Math.min(dp[j],dp[j-coins[i]]+1);
            }
        }


        return dp[amount]==Integer.MAX_VALUE ? -1 : dp[amount];
    }

    public int coinChange3(int[] coins, int amount) {

        int n = coins.length;

        int[] dp = new int[amount + 1];

        Arrays.fill(dp, Integer.MAX_VALUE);

        dp[0] = 1;
        for (int i = 0; i < n; i++) {
            for (int j = coins[i]; j <= amount; j++) {
                if (dp[j-coins[i]]!=Integer.MAX_VALUE){
                    dp[j]=Math.min(dp[j],dp[j-coins[i]]+1);
                }
            }
        }


        return dp[amount]==Integer.MAX_VALUE ? -1 : dp[amount];
    }

    public int coinChange2(int[] coins, int amount) {

        int[] dp = new int[amount + 1];

        for (int i = 0; i < dp.length; i++) {
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j <= amount; j++) {
                //当前的coins[i]=5,容量j为8，那么j-coins[i]=3。但是在容量j为3时，当前的coins可能无法把容量3装满
                if (dp[j - coins[i]] != Integer.MAX_VALUE) {
                    //如果此时coins[i]=5，上一次循环的coins[i]=3。
                    //dp[j]表示用上一次循环的coin来组成容量为j的硬币数
                    //dp[j-coins[i]]+1表示，用当前的coins表示组成容量为j的硬币数
                    dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
                }
            }
        }
        return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
    }

    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount + 1];

        for (int j = 0; j < dp.length; j++) {
            dp[j] = Integer.MAX_VALUE;
        }

        dp[0] = 0;

        Arrays.sort(coins);

        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j <= amount; j++) {
                if (dp[j - coins[i]] != Integer.MAX_VALUE) {
                    dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
                }
            }
        }

        return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
    }


}
